import * as mobx from 'mobx';
import { WeaTools } from  'ecCom';
import { ListStore } from './listStore';
import { WeaForm } from 'comsMobx';
import * as WORKFLOW_AGENT from '../apis/agent';

const { observable, action } = mobx;

export class WFTemplateManageStore extends ListStore {

    @observable templateManageVisible = false;    //模板管理弹出框

    constructor () {
        super();
        this.initDatas = this.initDatas.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.templateManage = this.templateManage.bind(this);
        this.setConditionByTree = this.setConditionByTree.bind(this);
        this.reload = this.reload.bind(this);
        this.clearFormFields = this.clearFormFields.bind(this);
        this.clearStatus = this.clearStatus.bind(this);
    }

    @action

    initDatas () {
        WORKFLOW_AGENT.manageHighSearch().then((datas)=>{
            this.condition = datas.condition;
            this.form && !this.form.isFormInit && this.form.initFormFields(datas.condition);
        })
    }

    doSearch (params = {}) {
        const searchParamsAd = this.form.getFormParams();
        const newParams = {...searchParamsAd,...params};
        WORKFLOW_AGENT.manageList(newParams).then((datas)=>{
            const { sessionkey } = datas;
            this.tableStore.getDatas(sessionkey, params.current || 1);
        });
    }

    templateManage(visible = false,params = {}){
        if(!visible){
            this.templateManageVisible = false;
            this.clearStatus();
        }else {
            this.form = new WeaForm();
            WORKFLOW_AGENT.manageHighSearch().then((datas)=>{
                this.condition = datas.condition;
                this.form.initFormFields(datas.condition);
            })
            WORKFLOW_AGENT.manageList().then((datas)=>{
                const { sessionkey } = datas;
                this.tableStore.getDatas(sessionkey, params.current || 1);
                this.templateManageVisible = true;
            });
        }
    }

    reload () {
        const current = this.tableStore.current;
        this.doSearch({current});
    }

    clearFormFields () {
        this.form.resetConditionValue();      //清除查询条件值
    }

    clearStatus () {
        this.showSearchAd = false;
        this.clearFormFields();
        this.resetTable();
    }
    setConditionByTree () {
        return 0;
    }
}